package com.ht.shop.service.impl;

import com.ht.shop.dao.GoodsDao;
import com.ht.shop.dao.impl.GoodsDaoImpl;
import com.ht.shop.pojo.PageBean;
import com.ht.shop.pojo.TbGoods;
import com.ht.shop.pojo.TbGoodsKz;
import com.ht.shop.service.GoodsService;

import java.util.List;

public class GoodsServiceImpl implements GoodsService {
    private GoodsDao goodsDao = new GoodsDaoImpl();
    @Override
    public void addGoods(TbGoodsKz tbGoodsKz) throws Exception {

        long kzid = goodsDao.addGoodsKz(tbGoodsKz);

        long itemid = goodsDao.addGoods(kzid);
        System.out.println("快照"+kzid+"商品"+itemid);
    }
    @Override
    public PageBean<TbGoods> getGoodsPageByTypeId(int typeId, int currentPage) throws Exception {
        PageBean<TbGoods> pageBean = new PageBean<>();
        //查询所有的数量
        int goodsCountByTypeId = goodsDao.getGoodsCountByTypeId(typeId);
        //先算出来当前应该加载的数据是哪些先算出来当前应该加载的数据是哪些,然后进行查询 sql
        int pageSize = pageBean.getPageSize();//每页的数量
        List<TbGoods> goodsList = goodsDao.getGoodsByTypeIdAndLimit(typeId, (currentPage - 1) * pageSize, pageSize);
        pageBean.setTotalPage(goodsCountByTypeId);//设置总条数
        pageBean.setList(goodsList);//设置查询到数据
        System.err.println(goodsCountByTypeId);
        return pageBean;
    }

    @Override
    public TbGoods getGoodById(int id) {
        if (id<=0) {
            //id 不存在,不查询
            return null;
        }
        return goodsDao.getGoodById(id);
    }

}
